package xin.zhiyin.控制.front;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.zhiyin.实体.书Entity;
import xin.zhiyin.实体.书章节Entity;
import xin.zhiyin.实体.书章节内容Entity;
import xin.zhiyin.实体.系统菜单Entity;
import xin.zhiyin.工具.ResponseBo;
import xin.zhiyin.服务.书.书Service;
import xin.zhiyin.服务.书.书章节Service;
import xin.zhiyin.服务.书.书章节内容Service;
import xin.zhiyin.服务.系统菜单Service;

import java.util.*;

@Controller
@RequestMapping(path = "/details")//页面的url地址
public class 详情页Controller {
    @Autowired
    private 书Service 书的Service;
    @Autowired
    private 书章节Service 章节Service;
    @Autowired
    private 书章节内容Service 章节内容Service;
    @Autowired
    private 系统菜单Service 菜单Service;
    @GetMapping(path = "/main")
    public String 系统后台概览(Model model){
        model.addAttribute("captchaType","math");
        return "system/main";
    }
    @RequestMapping({"","/","/index"})
    public String 书籍详情页(Long bookId , Model model){
        书Entity 书籍 = 书的Service.根据Id获取书(bookId);

        List<书章节Entity> 章节列表 = new ArrayList<书章节Entity>();
        章节列表 = 章节Service.根据BookId获取书章节(bookId);

        model.addAttribute("book",书籍);
        model.addAttribute("chapterList",章节列表);
        return "front/details/details";
    }

    @GetMapping(path = "/getBook")
    public String 章节列表页面( Long bookId , Model model)
    {
        model.addAttribute("bookId",bookId);
        return "front/details/index";
    }
    @RequestMapping("/add")
    @ResponseBody
    public ResponseBo 添加书章节(书章节Entity 章节){
        书章节Entity 保存的对象 = 章节Service.保存并返回新建对象(章节);
        书章节内容Entity 内容 = new 书章节内容Entity();
        内容.setId(保存的对象.getId());
        章节内容Service.保存(内容);
        return ResponseBo.ok(保存的对象);
    }
    @RequestMapping("/getBookChapter")
    @ResponseBody
    public ResponseBo 根据id获取书章节(Long chapterId) {
        书章节Entity 章节 = 章节Service.根据Id获取书章节(chapterId);
        return ResponseBo.ok(章节);
    }
    @RequestMapping("/getContentById")
    public String 根据id获取章节内容(Long contentId, Model model)
    {
        书章节内容Entity 内容 = 章节内容Service.根据Id获取书章节内容(contentId);
        model.addAttribute("content",内容);
        return "front/details/content";
    }
    @RequestMapping("/getContentByIdr")
    @ResponseBody
    public ResponseBo 根据id获取章节内容(Long contentId)
    {
        书章节内容Entity 内容 = 章节内容Service.根据Id获取书章节内容(contentId);
        return ResponseBo.ok(内容);
    }
    @RequestMapping("/update")
    @ResponseBody
    public ResponseBo 更新书章节(书章节Entity 章节) {
        章节Service.更新(章节);
        return ResponseBo.ok("更新章节成功");
    }
    @RequestMapping("/updateChapterContent")
    @ResponseBody
    public ResponseBo 更新书章节内容(书章节内容Entity 章节内容) {
        章节内容Service.更新(章节内容);
        return ResponseBo.ok("章节内容更新成功");
    }
    @RequestMapping("/listData")
    @ResponseBody
    public String 获取所有书章节列表数据(Long bookId) {

        书章节Entity 章节;
        Iterator<书章节Entity> 章节Iter = 章节Service.根据BookId获取书章节(bookId).iterator();
        List<LinkedHashMap<String, Object>> 章节List = new ArrayList<>();
        while(章节Iter.hasNext())
        {
            LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            章节 = 章节Iter.next();
            map.put("id",章节.getId());
            map.put("pId",章节.getParentId());
            map.put("bookId",章节.getBookId());
            map.put("name",章节.getTitle());
            map.put("sort",章节.getSort());
            章节List.add(map);
        }
        String josn类目List = JSON.toJSONString(章节List);
//        model.addAttribute("systemMenuList",josn菜单List);
        return josn类目List;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseBo 批量删除书章节(String chapterIds) {
        List<String> ids = Arrays.asList(chapterIds.split(","));
        List<Long> LString = new ArrayList();
        for(String st:ids)
        {
            LString.add(Long.valueOf(st));
        }
        章节Service.批量删除(LString);
        return ResponseBo.ok(LString);
    }
}
